<template>
  <div>
    <header>
      <div>
        <span>TODOLIST</span>
        <!-- enter事件修饰符 -->
        <input
          type="text"
          v-model="inputVal"
          placeholder="添加toDo"
          @keydown.enter="addDo"
        />
      </div>
    </header>
    <div class="container">
      <div class="tasks">
        <div class="title-box">
          <h3>正在进行</h3>
          <span class="run-count">{{runNum}}</span>
        </div>
        <ul class="running-tasks-box">
        <!-- list 里面是包含 正在进行和已完成的数据 -->
          <template v-for="(item,index) in list">
              <!-- item.flag==false 满足这样一个等式 就会返回一个 true -->
            <li class="list-item" v-if="!item.flag" :key="index">
            <div>
              <input type="checkbox" v-model="item.flag"/>
              <span>{{item.msg}}</span>
            </div>
            <div class="del" @click="del(index)">-</div>
          </li>
          </template>
        </ul>
      </div>
      <div class="tasks">
        <div class="title-box">
          <h3>已经完成</h3>
          <span class="finish-count">{{finishNum}}</span>
        </div>
        <ul class="finish-task-box">
           <template v-for="(item,index) in list">
            <li class="list-item" v-if="item.flag" :key="index">
            <div>
              <input type="checkbox" v-model="item.flag"/>
              <span>{{item.msg}}</span>
            </div>
            <div class="del" @click="del(index)">-</div>
          </li>
          </template>
        </ul>
      </div>
    </div>
  </div>
</template>
<script>
// 注意：filter(callback);callback默认有三个参数，分别为value，index，self。
// let arr = [1,2,3,4,5,6];
// // let arr1 = arr.filter((item,index,self)=>{
// //     console.log(item) // -> 1,2,3,4,5,6
// //     // console.log(index) // -> 0,1,2,3,4,5
// //     // console.log(self) // -> [1,2,3,4,5,6]
// //     return item > 3
// // })
// let arr1 = arr.filter(item => item>3)
// console.log(arr1) // -> [4,5,6]
export default {
  data() {
    return {
      inputVal: "",
      list:[], // 存放任务的
    };
  },
  computed:{ // 计算属性
    runNum(){
      return this.list.filter(item => !item.flag).length;
    },
    finishNum(){
      return this.list.filter(item => item.flag).length;
    }
  },
  methods: {
    addDo() {
      // 添加的事件
      //  flag false 代表 正在进行 true 代表已经完成
      this.list.push({flag:false,msg:this.inputVal});
      this.inputVal = '';
    },
    del(i){ // 删除
      this.list.splice(i,1);
    }
  },
};
</script>
<style lang="scss" scoped>
* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
}

#app {
  width: 100%;
  height: 100%;
  background-color: #cdcdcd;
}

header {
  width: 100%;
  height: 50px;
  background-color: #444;
}

header div {
  width: 600px;
  margin: 0 auto;
  height: 100%;
  /* 将元素盒转化为弹性盒子 */
  display: flex;
  /* 
            justify-content 主轴方向上的对齐方式
            space-between 两边对齐 中间留白
             */
  justify-content: space-between;
  /* 侧轴的对齐方式 */
  align-items: center;
  color: #fff;
}

header div span {
  font-size: 30px;
  font-weight: bold;
}

header div input {
  outline: none;
  border: none;
  width: 300px;
  height: 30px;
  padding-left: 15px;
  border-radius: 6px;
}

.container {
  width: 100%;
}

.tasks {
  width: 600px;
  margin: 0 auto;
  margin-top: 50px;
}

ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.title-box {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

h3 {
  font-size: 26px;
  font-weight: bold;
}

.title-box span {
  width: 20px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  background-color: #fff;
  border-radius: 20px;
}

.list-item {
  width: 100%;
  height: 40px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #fff;
  border-left: 4px solid #629a9c;
  border-radius: 6px;
  padding-left: 10px;
  padding-right: 15px;
  margin-bottom: 20px;
}

.list-item input {
  height: 30px;
  width: 30px;
  vertical-align: middle;
}

.list-item span {
  display: inline-block;
  vertical-align: middle;
}

.del {
  box-sizing: content-box;
  display: inline-block;
  width: 14px;
  height: 12px;
  border-radius: 14px;
  border: 6px double #fff;
  background: #ccc;
  line-height: 14px;
  text-align: center;
  color: #fff;
  font-weight: bold;
  font-size: 14px;
  cursor: pointer;
}

.finish-task-box {
  opacity: 0.5;
}
</style>
